package com.yhgm.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author moon
 * @description
 * @date 2023/2/28
 */
@Slf4j
public class ZipUtils {
    public static void toZip(List<File> srcFiles, File zipFile) throws IOException {
        if(zipFile==null){
            return;
        }
        if(!zipFile.getName().endsWith(".zip")){
            return;
        }
        ZipOutputStream zos=null;
        FileOutputStream out=new FileOutputStream(zipFile);
        log.info("srcFiles.size():"+srcFiles.size());
        try {
            zos=new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf=new byte[1024];
                log.info("srcFile.getName():"+srcFile.getName());
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                // 读取文件并写入到zip中
                //不打印，节省时间
                //System.out.println(srcFile.getName());
                FileInputStream in;
                try {
                    in=new FileInputStream(srcFile);
                }catch (Exception e) {
                    //to do:add log
                    continue;
                }
                while ((len=in.read(buf)) !=-1) {
                    zos.write(buf, 0, len);
                    zos.flush();
                }
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (zos !=null) {
                zos.close();
            }
        }
    }
//    public void export(HttpServletRequest request, HttpServletResponse response) throws IOException{
//        OutputStream out = response.getOutputStream();
//        try {
//            response.setCharacterEncoding("UTF-8");
//            String path = searchService.export();
//            File file = new File(path);
//            byte[] data = getStream(path);
//            // 压缩包名称
//            String downloadName = file.getName();
//            response.setHeader("Content-Disposition",
//                    "attachment;filename=" + URLEncoder.encode(downloadName, "utf-8"));
//            response.addHeader("Content-Length", "" + data.length);
//            response.setContentType("application/octet-stream;charset=UTF-8");
//            IOUtils.write(data, out);
//            if(!file.delete()){
//                file.deleteOnExit();
//            }
//        } catch (IOException ex) {
//            ex.printStackTrace();
//        }finally {
//            try{
//                if (out != null) {
//                    out.flush();
//                    out.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    private byte[] getStream(String filePath) {
//        byte[] buffer = null;
//        File file = new File(filePath);
//        FileInputStream fis = null;
//        ByteArrayOutputStream bos = null;
//        try {
//            bos = new ByteArrayOutputStream();
//            fis = new FileInputStream(file);
//            byte[] b = new byte[1024];
//            int n;
//            while ((n = fis.read(b)) != -1) {
//                bos.write(b, 0, n);
//            }
//            buffer = bos.toByteArray();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                if(fis!=null)
//                    fis.close();
//                if(bos!=null)
//                    bos.close();
//            }catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return buffer;
//    }
}
